package com.tengyun.middleplatform.trade.tradedata.util;

import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RAtomicLong;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.util.StringUtils;

import java.util.Optional;
import java.util.concurrent.TimeUnit;

/**
 * @Author: likailun
 * @Description:
 * @Date: create in 2022/8/26 5:36 PM
 */
@Slf4j
public class CacheUtils {
    // aike授权信息
    private static final String AIKE_INTERFACE_AUTH_TOKEN = "AIKE_INTERFACE_AUTH_TOKEN";

    private static RedissonClient redissonClient = ServiceLocator.getBean(RedissonClient.class);


    /**
     * 设置 艾客接口授权token
     *
     * @param token
     * @param secondToLive 过期时间（秒）
     */
    public static void setAikeAuthToken(String token,long secondToLive) {
        RBucket<String> bucket = redissonClient.getBucket(AIKE_INTERFACE_AUTH_TOKEN);
        bucket.set(token, secondToLive, TimeUnit.SECONDS);
    }

    /**
     * 获取 艾客接口授权token
     *
     * @return token
     */
    public static Optional<String> getAikeAuthToken() {
        RBucket<String> bucket = redissonClient.getBucket(AIKE_INTERFACE_AUTH_TOKEN);
        String payParams = bucket.get();
        if (!StringUtils.isEmpty(payParams)) {
            return Optional.of(payParams);
        }
        return Optional.empty();
    }

}
